<?php

function laporan_pensiun($periode_id=0){
	my_set_code_js('
		function postDataPeriode(periode_id){ 
			location.href=\'index.php?com='.$_GET['com'].'&periode_id=\'+periode_id ;
		}
	');

	if($periode_id==0){
		$periode_id=get_periode_gaji() ;
	}	
	$query_check_periode = " SELECT * FROM pr_periode ORDER BY periode_id DESC";
	$result = my_query($query_check_periode);
	$prs = array();
	while($prow = my_fetch_array($result)){
		$prs[$prow['periode_id']] = date('m-Y',strtotime($prow['tanggal']));
	}
	$fperiode_id = array(
		'id'=>'periode_id',
		'value'=>$periode_id ,
		'name'=>'periode_id',
		'onchange'=>'javascript:postDataPeriode(this.value)'
	);
	$src ="<form method=\"GET\">
	<input type=\"hidden\" name=\"com\" value=\"{$_GET['com']}\" />
	<table border=\"0\" width=\"100%\"> <tr><td width='10%'>
	Periode&nbsp;:</td><td width='10%'>".form_dropdown($fperiode_id,$prs)." </td>
	</tr></table></form>";

	$header = array(
		'#'=>array('style'=>'text-align:center;border-bottom:2px solid;width:5%'),  
		'NIK'=>array('style'=>'border-bottom:2px solid;width:10%'),  
		'Nama karyawan'=>array('style'=>'border-bottom:2px solid;width:25%'),  
		'TMB'=>array('style'=>'text-align:center;border-bottom:2px solid;width:5%'),  
		'Masa Kerja'=>array('style'=>'border-bottom:2px solid;width:5%'),  
		'Gaji pokok terakhir'=>array('style'=>'text-align:right;border-bottom:2px solid;width:15%'),  
		'Uang Pensiun'=>array('style'=>'text-align:right;border-bottom:2px solid;width:15%'),  
		'Nomor SK'=>array('style'=>'text-align:center;border-bottom:2px solid;width:10%'),  
		'TMT'=>array('style'=>'text-align:center;border-bottom:2px solid;width:10%'),  
	);
	
	$periode = my_get_data_by_id('pr_periode','periode_id',$periode_id);

	$query = "SELECT * FROM pemberhentian a
		INNER JOIN karyawan b ON a.karyawan_id = b.karyawan_id
		INNER JOIN sk_pemberhentian c ON c.sk_id = a.sk_id
		WHERE c.status_henti ='Pensiun'
		AND (a.tmt BETWEEN '{$periode['dari']}' AND '{$periode['sampai']}' )";	
	
 
	$result = my_query($query );
 
	$total_records = my_num_rows($result );
	$scroll_page =5;  
	$per_page =25;  
	$current_page = isset($_GET['page']) ? (int) $_GET['page'] : 1 ; 
	if($current_page < 1){
		$current_page = 1;
	}		 
	$pager_url  ="index.php?com={$_GET['com']}&task={$_GET['task']}&periode_id={$_GET['periode_id']}&page=";	 
	$inactive_page_tag = 'style="padding:4px;background-color:#BBBBBB"';  
	$previous_page_text = ' Mundur '; 
	$next_page_text = ' Maju ';  
	$first_page_text = ' Awal '; 
	$last_page_text = ' Akhir ';
	
	$kgPagerOBJ = new kgPager();
	$kgPagerOBJ->pager_set(
		$pager_url, 
		$total_records, 
		$scroll_page, 
		$per_page, 
		$current_page, 
		$inactive_page_tag, 
		$previous_page_text, 
		$next_page_text, 
		$first_page_text, 
		$last_page_text ,$pager_url_last); 
	 		
	$result = my_query($query ." LIMIT ".$kgPagerOBJ->start.", ".$kgPagerOBJ->per_page); 	
	$row = array(); 
	$i = ($current_page  - 1 ) * 25; 
	while($ey = my_fetch_array($result)){
		$i++;
		$karyawan =  loaddata_karyawan($ey['karyawan_id']);  

		$row[] = array(
			'#'=>position_text_align ($i, 'center'),  
			'nik'=>  strtoupper($karyawan['nik'] ),  
			'nama'=>  $karyawan['nama_gelar'] ,  
			'tmb'=>  position_text_align( date('d-m-Y',strtotime($karyawan['tanggal_diterima'] )),'center'),  
			'mk'=>  position_text_align( getlamakerja($ey['tmt'] , $karyawan['tanggal_diterima']) ,'center'),  
			'gapok'=>   position_text_align( rp_format(get_gaji_karyawan_laporan_mpp($ey['karyawan_id']))  , 'right'),   
			'up'=>  position_text_align( rp_format($ey['nominal'])  , 'right'),   
			'sk'=>   position_text_align( strtoupper($ey['nomor'] ),'center'),   
			'tmt'=>  position_text_align( date('d-m-Y',strtotime($ey['tmt'] )),'center'),  
		);
	}
	$datas = table_rows($row);  
	$menuontop = array(  
		'Cetak' => array('onclick'=>'javascript:window.open(\'index.php?com='.$_GET['com'].'&task=plaincetak&periode_id='.$periode_id.'\',\'mywin\',\'left=20,top=20,width=800,height=600,toolbar=0,scrollbars ,resizable=0\');'),
		'Excel'=>array('onclick'=>'javascript:location.href=\'index.php?com='.$_GET['com'].'&task=excel&periode_id='.$periode_id.'\';'),
	);
	$printmenu = additional_menu_on_list( $menuontop ,$src);
	$paging = $kgPagerOBJ ->showPaging();
	return $printmenu .table_builder($header , $datas ,  4, false,$paging );
}


function get_gaji_karyawan_laporan_mpp($karyawan_id){
	$query_get_gaji = "SELECT gapok_km FROM kekaryawanan WHERE karyawan_id = {$karyawan_id} ";
	$res = my_query($query_get_gaji);
	$rw = my_fetch_array($res);
	$da = $rw['gapok_km'] ;
	 
	return ($da);
}


function print_plain($periode_id=0){

	$periode = my_get_data_by_id('pr_periode','periode_id',$periode_id);

	$query = "SELECT * FROM pemberhentian a
		INNER JOIN karyawan b ON a.karyawan_id = b.karyawan_id
		INNER JOIN sk_pemberhentian c ON c.sk_id = a.sk_id
		WHERE c.status_henti ='Pensiun'
		AND (a.tmt BETWEEN '{$periode['dari']}' AND '{$periode['sampai']}' )";		
 
	$result = my_query($query ); 
	$datas = array();  
	$i=1;

	$datas[] = array(
		 'k1'=>array("NO" ,5 ,STR_PAD_BOTH),
		 'k2'=>array("NIK" ,10,STR_PAD_RIGHT),
		 'k3'=>array("Nama karyawan" ,20,STR_PAD_RIGHT),
		 'k4'=>array("TMB" ,10,STR_PAD_BOTH),  
		 'k5'=>array("Masa Kerja" ,10,STR_PAD_BOTH),  
		 'k6'=>array("Gaji pokok terakhir",10,STR_PAD_LEFT),  
		 'k7'=>array("Uang Pensiun" ,15,STR_PAD_LEFT),  
		 'k8'=>array("Nomor SK" ,10,STR_PAD_BOTH),  
		 'k9'=>array("TMT" ,10,STR_PAD_BOTH),  
	); 
	$datas[] = array(
		 'k1'=>array(" " ,100 ,STR_PAD_BOTH),
	); 
	$i=0;
	while($ey = my_fetch_array($result)){

		$karyawan =  loaddata_karyawan($ey['karyawan_id']);  		
		$i++; 
		$datas[] = array(		 
			 'k1'=>array($i ,5 ,STR_PAD_BOTH),
			 'k2'=>array(strtoupper($karyawan['nik']) ,10,STR_PAD_RIGHT),
			 'k3'=>array($karyawan['nama_gelar'],20,STR_PAD_RIGHT),
			 'k4'=>array(date('d-m-Y',strtotime($karyawan['tanggal_diterima'] )) ,10,STR_PAD_BOTH),  
			 'k5'=>array(getlamakerja($ey['tmt'] , $karyawan['tanggal_diterima']) ,10,STR_PAD_BOTH),  
			 'k6'=>array(rp_format(get_gaji_karyawan_laporan_mpp($ey['karyawan_id'])) ,10,STR_PAD_LEFT),  
			 'k7'=>array(rp_format($ey['nominal']) ,15,STR_PAD_LEFT),  
			 'k8'=>array(strtoupper($ey['nomor'] ) ,10,STR_PAD_BOTH),  
			 'k9'=>array(date('d-m-Y',strtotime($ey['tmt'] )) ,10,STR_PAD_BOTH),    
		);
	}
	$view = 	str_pad("LAPORAN PENSIUN",100," ",STR_PAD_BOTH);
	return $view ."\n"."\n".set_txt_report($datas);
}


function excel_report($periode_id=0){
	my_component_load('xl_builder' , false);
	$header = array( 
		'#'=>array('style'=>'text-align:center;border-bottom:2px solid;width:5%'),  
		'NIK'=>array('style'=>'border-bottom:2px solid;width:10%'),  
		'Nama karyawan'=>array('style'=>'border-bottom:2px solid;width:25%'),  
		'TMB'=>array('style'=>'text-align:center;border-bottom:2px solid;width:5%'),  
		'Masa Kerja'=>array('style'=>'border-bottom:2px solid;width:5%'),  
		'Gaji pokok terakhir'=>array('style'=>'text-align:right;border-bottom:2px solid;width:15%'),  
		'Uang Pensiun'=>array('style'=>'text-align:right;border-bottom:2px solid;width:15%'),  
		'Nomor SK'=>array('style'=>'text-align:center;border-bottom:2px solid;width:10%'),  
		'TMT'=>array('style'=>'text-align:center;border-bottom:2px solid;width:10%'),  
	);
	$periode = my_get_data_by_id('pr_periode','periode_id',$periode_id);

	$query = "SELECT * FROM pemberhentian a
		INNER JOIN karyawan b ON a.karyawan_id = b.karyawan_id
		INNER JOIN sk_pemberhentian c ON c.sk_id = a.sk_id
		WHERE c.status_henti ='Pensiun'
		AND (a.tmt BETWEEN '{$periode['dari']}' AND '{$periode['sampai']}' )";		
 
	$result = my_query($query);
	while($ey = my_fetch_array($result )){
		$i++;
		$karyawan =  loaddata_karyawan($ey['karyawan_id']);  

		$row[] = array(
			'#'=>position_text_align ($i, 'center'),  
			'nik'=>  strtoupper($karyawan['nik'] ),  
			'nama'=>  $karyawan['nama_gelar'] ,  
			'tmb'=>  position_text_align( date('d-m-Y',strtotime($karyawan['tanggal_diterima'] )),'center'),  
			'mk'=>  position_text_align( getlamakerja($ey['tmt'] , $karyawan['tanggal_diterima']) ,'center'),  
			'gapok'=>   position_text_align( rp_format(get_gaji_karyawan_laporan_mpp($ey['karyawan_id']))  , 'right'),   
			'up'=>  position_text_align( rp_format($ey['nominal'])  , 'right'),   
			'sk'=>   position_text_align( strtoupper($ey['nomor'] ),'center'),   
			'tmt'=>  position_text_align( date('d-m-Y',strtotime($ey['tmt'] )),'center'),  
		); 
	}   
	$datas = table_rows_excel($row); 
	return table_builder_excel($header , $datas , 6 ,false ); 
}